package com.wu.controller;

import com.wu.error.BusinessException;
import com.wu.error.EnumBusinessError;
import com.wu.response.CommonReturnType;
import com.wu.service.UsersService;
import com.wu.util.MyUtil;
import com.wu.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-04-20 17:48
 */
@RestController
@Api(tags = "登录注册有关的事件")
public class LoginController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private MyUtil myUtil;

    /**
     * 登录请求
     *
     * @param telPhone
     * @param password
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "telPhone", value = "电话号码", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataTypeClass = String.class)
    })
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonReturnType login(@RequestParam(name = "telPhone") String telPhone,
                                  @RequestParam(name = "password") String password) throws Exception {
        Map<String, Object> login = usersService.login(telPhone, password);
        return CommonReturnType.create(login);
    }

    /**
     * 退出登录
     *
     * @throws Exception
     */
    @ApiOperation(value = "退出登录")
    @RequestMapping(value = "/loginOut", method = RequestMethod.POST)
    public CommonReturnType loginOut() {
        tokenUtils.removeToken();
        return CommonReturnType.create(null);
    }


    /**
     * 验证token
     *
     * @return
     */
    @ApiOperation(value = "验证token（检验用户是否登录）")
    @RequestMapping(value = "/checkToken", method = RequestMethod.POST)
    public CommonReturnType checkToken() {
        String token = tokenUtils.parseTokenToken();
        if (StringUtils.isEmpty(token)) {
            return CommonReturnType.create(false);
        } else {
            boolean b = tokenUtils.inspectionToken(token);
            if (b) {
                return CommonReturnType.create(true);
            } else {
                return CommonReturnType.create(false);
            }
        }
    }

    /**
     * 注册用户
     *
     * @param password
     * @param telPhone
     * @param sms
     * @return
     */
    @ApiOperation(value = "注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "telPhone", value = "电话号码", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "sms", value = "短信验证码", required = true, dataTypeClass = String.class)
    })
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public CommonReturnType register(@RequestParam("password") String password,
                                     @RequestParam("telPhone") String telPhone,
                                     @RequestParam("sms") String sms
    ) {
        if (!myUtil.checkSMSIsOk(telPhone, sms)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR, "验证码错误");
        }
        usersService.register(password, telPhone);
        return CommonReturnType.create(null);
    }

}
